package com.org.cys.service.impl.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.org.cys.dao.base.IGeneralDao;
import com.org.cys.pojo.base.Page;
import com.org.cys.service.system.UserRoleService;
import com.org.cys.service.system.UserService;
import com.org.cys.util.IdWorker;
import com.org.cys.util.MD5;
import com.org.cys.util.MapUtil;
import com.org.cys.util.PageData;
import com.org.cys.util.StringUtil;


/**
 *	t_system_user Service实现类
 *	@author caiyushen
 */
@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
  
  @Resource(name="generalDao")
  private IGeneralDao generalDao;
  
  @Resource(name = "userRoleServiceImpl")
  private UserRoleService userRoleService;
  
  public static String XMLMapper = "UserMapper";
  
  //根据用户id获取用户信息
  public static String findByIdStatement = XMLMapper + ".findById";
  
  //根据用户id修改密码
  public static String updatePasswordStatement = XMLMapper + ".updatePassword";
  
  
  /**
	* 装载User
	* @param PageData
	* @return
	* @throws Exception 
	* */
  @Override
  public PageData getEntityById(PageData pd) throws Exception {
	  return (PageData) generalDao.findForObject(findByIdStatement, pd);
  }
  
  
  /**
   * 装载User
   * @param PageData
   * @return
   * @throws Exception 
   * */
  @Override
  public PageData getEntityById(Long id) throws Exception{
	  PageData pd = new PageData();
	  pd.put("id", id);
	  return (PageData) generalDao.findForObject(findByIdStatement, pd);
  }
  
  /**
	* 保存User
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void save(PageData pd) throws Exception{
	  pd.put("id",IdWorker.getId());
	  pd.put("createTime", new Date());
	  pd.put("updateTime", new Date());
	  generalDao.save(XMLMapper+".save",pd);
  }
  
  /**
   * 批量保存User
   * @param PageData
   * @throws Exception
   * */
  public void saveBatch(List<PageData> pdList) throws Exception{
	  Date nowDateTime = new Date();
	  for(PageData pd : pdList){
		  pd.put("id", IdWorker.getId());
		  pd.put("createTime", nowDateTime);
		  pd.put("updateTime", nowDateTime);
	  }
	  generalDao.batchSave(XMLMapper+".saveBatch",pdList);
  }
  
  /**
	* 更新User
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void update(PageData pd) throws Exception{
	  pd.put("updateTime", new Date());
	  generalDao.update(XMLMapper+".update",pd);
  }
  
  /**
   * 修改密码
   * @param PageData
   * @throws Exception
   * */
  public void updatePassword(PageData pd) throws Exception{
	  pd.put("updateTime", new Date());
	  generalDao.update(updatePasswordStatement,pd);
  }
  
  /**
    * 修改密码
	* @param userId 用户id
	* @param userPassword 用户密码(已加密)
	* @throws Exception
	* */
  public void updatePassword(Long userId,String userPassword) throws Exception{
	  PageData pd = new PageData();
	  pd.put("userPassword",userPassword);
	  pd.put("id",userId);
	  pd.put("updateTime", new Date());
	  generalDao.update(updatePasswordStatement,pd);	 
  }
 
  /**
	* 删除User
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void delete(PageData pd) throws Exception{
	  //先删除中间表数据
	  userRoleService.deleteByUserId(MapUtil.getLong(pd, "id"));
	  generalDao.delete(XMLMapper+".delete", pd);
  }
  
  /**
	* User列表
	* @param PageData
	* @throws Exception
	* */
  @Override
  public List<PageData> dataList(Page page) throws Exception{
	  return (List<PageData>) generalDao.findForList(XMLMapper+".dataListPage", page);
  }
  
  /**
	* 获取所有User数据
	* @param PageData
	* @throws Exception
	* */
  @Override
  public List<PageData> listAll(PageData pd) throws Exception{
	  return (List<PageData>) generalDao.findForList(XMLMapper+".listAll", pd);
  }
  
  /**
	* 批量删除User
	* @param ArrayDATA_IDS[]
	* @throws Exception
	* */
  @Override
  public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
	  //先删除中间表数据
	  userRoleService.deleteByUserIds(ArrayDATA_IDS);
	  generalDao.delete(XMLMapper+".deleteAll", ArrayDATA_IDS);
  }
  
  /**
   * 根据用户账号获取User
   * @param userAccount 用户账号
   * @throws Exception
   * */
  @SuppressWarnings("unchecked")
  public List<PageData> getListByAccount(String userAccount) throws Exception{
	  List<PageData> dataList = new ArrayList<>();
	  if(StringUtil.checkObj(userAccount)){
		  dataList = (List<PageData>) generalDao.findForList(XMLMapper+".getListByAccount", userAccount);
	  }
	  return dataList;
	  
  }
  
}